﻿CREATE PROCEDURE [dbo].[proc_Assets_Getlist_TongJiXX_G]
	(
		@CompanyId NVARCHAR(200),
		@CategoryId int,
		@NameId int,
		@StateId int,
		@StartIndex int,
		@EndIndex INT

	)
AS
	DECLARE @sql VARCHAR(5000)
	SET @sql = '
	;WITH list as (Select ROW_NUMBER() OVER (ORDER BY a.Id)AS Row,
	a.Id,
	a.GId,
	a.CategoryId,
	(Select Title From Assets_Categorys Where Id=a.CategoryId) As CategoryName,
	NameId,
	(Select Title From Assets_Names Where Id=a.NameId) As NameTitle,
	Description,
	CompanyId,
	(Select CompanyName From Company Where Id=a.CompanyId) As CompanyName,	
	UserName,
	StateId,
	(Select Title From Assets_States Where Id=a.StateId) As StateName,
	Prices,
	DJUserName,
	DJDate,
	GGXH,
	GZDate,
	IsNULL(IsApplyBf,0) AS IsApplyBf,
	ANumber,
	IsNull(LyDate,''1900-01-01'') as LyDate,
	ename+''(''+(Select DName From Department Where Id=a.DepartmentId)+'')'' as EName 
	From Assets a
	Where ('''+@CompanyId+''' = ''0'' OR a.CompanyId IN ('+@CompanyId+'))  and
	 ( a.NameId='+CONVERT(VARCHAR,@NameId)+') and ( a.StateId='+CONVERT(VARCHAR,@StateId)+')
	 
	) '
	
	SET @sql = @sql + 'Select * From list,(Select Count(Id) AS cid From list
	) AS n Where Row between ' + CONVERT(VARCHAR,@StartIndex) + ' and ' + CONVERT(VARCHAR,@EndIndex) + '
	'
	EXEC(@sql)
